package com.gjy.reflect;

import org.junit.jupiter.api.Test;
import org.springframework.core.DefaultParameterNameDiscoverer;

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-02-06 17:20:32
 */
public class ParameterTest {

    @Test
    public void test1()  throws Exception{
        Class<ParameterTest> clazz = ParameterTest.class;

        // javac -parameters
        Method method1 = clazz.getDeclaredMethod("method1", String.class);
        Parameter[] parameters = method1.getParameters();
        for (Parameter parameter : parameters) {
            System.out.println("parameter.getName() = " + parameter.getName());
        }

        // Spring 提供的方法
        DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer();
        String[] parameterNames = discoverer.getParameterNames(method1);
        System.out.println("parameterNames = " + Arrays.toString(parameterNames));
    }

    public void method1(String name) {
        System.out.println("name = " + name);
    }
}
